package com.smart.business.dao;

import com.smart.entity.business.*;
import com.smart.mybatis.dao.BaseDao;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;


/**
 * 点位配置
 *
 * @author wf
 * @since 2025-07-14 10:43:13
 */
@Mapper
public interface PointDao extends BaseDao<PointEntity> {

    List<PointEntity> getPointList(@Param("id") String id,
                                   @Param("meterTypeId") String meterTypeId,
                                   @Param("name") String name,
                                   @Param("code") String code);
    List<String> getListByPointId(@Param("pointId") String pointId);

    List<PointEntity> getListByCompanyNames(@Param("companyNames") List<String> permissionResult);

    List<CollectorEntity> allCollector(@Param("companyName") List<String> companyName);

    String getDictName(@Param("name") String name);

    List<UsedChannelVO> countUsedChannels(@Param("allCollectorIds") List<String> allCollectorIds);

    @Update("UPDATE point SET collector_id = #{collectorId} , status = '1' WHERE id = #{Id} AND is_deleted = '0' AND is_disable = '1'")
    void updateCollector(@Param("collectorId") String collectorId, @Param("Id") String Id);

    List<ChannelEntity> getUnConnectChannels(@Param("collectorId")String collectorId);


    void connectBatch(@Param("list") List<PointParameterEntity> parameterList);

    @Delete( "DELETE FROM point_parameter WHERE point_id = #{id}")
    void deleteConnect(String id);

    @Update("UPDATE point SET collector_id = null, status = '0' WHERE id = #{Id} AND is_deleted = '0' AND is_disable = '1'")
    void setDisconnect(@Param("Id") String id);
    /**
     * 插入点位设备关联信息
     *
     * @param id 点位ID
     * @param name 点位名称
     * @param collectorId 设备编号
     * @param companyName 公司名称
     */
    @Insert("INSERT INTO point_history(id,name, collector_id, company_name) " +
            "VALUES(#{id}, #{name}, #{collectorId}, #{companyName})")
    void recordCollector(@Param("collectorId") Long collectorId,@Param("name") String name, @Param("id") String id,@Param("companyName") String companyName);

    void deletePoints(@Param("ids") List<String> ids);

    /**
     * 查询点位数据
     *
     * @param pointId 点位ID
     * @param tableName 数据表名称
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param size 每页数量
     * @param offset 偏移量
     * @return 点位数据列表
     */
    @MapKey("id")
    List<Map<String, Object>> getList(
            @Param("pointId") String pointId,
            @Param("tableName") String tableName,
            @Param("startTime") String startTime,
            @Param("endTime") String endTime,
            @Param("size") int size,
            @Param("offset") int offset);

    int countList(
            @Param("pointId") String pointId,
            @Param("startTime") String startTime,
            @Param("endTime") String endTime,
            @Param("tableName") String tableName);
    @MapKey("id")
    List<Map<String, Object>> getListByType(
            @Param("pointIds") List<String> pointIds,
            @Param("startTime") String startTime,
            @Param("endTime") String endTime,
            @Param("frequency") String frequency);

    int countListByType(
            @Param("pointId") String pointId,
            @Param("startTime") String startTime,
            @Param("endTime") String endTime,
            @Param("frequency") String frequency,
            @Param("tableName") String tableName);

    @MapKey("id")
    List<Map<String, Object>> getPointMessages(@Param("companyNames")List<String> companyNames);
    @MapKey("id")
    List<Map<String, Object>> getBarChartData(@Param("meterTypeId") String meterTypeId,
                                              @Param("companyNames") List<String> companyNames,
                                              @Param("startTime") String startTime,
                                              @Param("endTime") String endTime);
    @MapKey("id")
    List<Map<String, Object>> getMeterTypeConsumption(@Param("companyName") List<String> companyNames);

    @MapKey("id")
    List<Map<String, Object>> getPieChartData(@Param("companyName") List<String> companyName);

    @MapKey("id")
    List<Map<String, Object>> getSubLevelConsumptionData(@Param("companyName") List<String> companyName,@Param("meterTypeId") String meterTypeId);
    @MapKey("id")
    List<Map<String,Object>> getCompanyMeterType(@Param("companyNames") List<String> companyNames);
    @MapKey("id")
    List<Map<String, Object>> getCompanyDayConsumption(@Param("companyName") List<String> companyName,
                                                       @Param("meterTypeId") String meterTypeId,
                                                       @Param("dayStart") String dayStart,
                                                       @Param("dayEnd") String dayEnd);
    @MapKey("id")
    List<Map<String, Object>> getCompanyWeekConsumption(@Param("companyName") List<String> companyName,
                                                       @Param("meterTypeId") String meterTypeId,
                                                       @Param("weekStart") String weekStart,
                                                       @Param("weekEnd") String weekEnd);
    @MapKey("id")
    List<Map<String, Object>> getCompanyMonthConsumption(@Param("companyName") List<String> companyName,
                                                       @Param("meterTypeId") String meterTypeId,
                                                       @Param("monthStart") String monthStart,
                                                       @Param("monthEnd") String monthEnd);
    @Select("SELECT id FROM point WHERE meter_type_id = #{meterTypeId} AND is_deleted = '0' AND is_disable = 1")
    List<String> getPointIdsByMeterTypeId(@Param("meterTypeId") String meterTypeId);

    String getUnit(@Param("tableName") String tableName);
}
